// https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
// 160. 相交链表
// 给你两个单链表的头节点 headA 和 headB，请你找出并返回两个单链表相交的起
// 始节点。如果两个链表不存在相交节点，返回 null
import { ListNode } from '../utils/index.ts';

function getIntersectionNode(
    headA: ListNode | null,
    headB: ListNode | null
): ListNode | null {
    if (headA === null || headB === null) return null;
    let pA: ListNode | null = headA,
        pB: ListNode | null = headB;
    while (pA !== pB) {
        pA = pA === null ? headB : pA.next;
        pB = pB === null ? headA : pB.next;
    }
    return pA;
}
